/**
 * 
 */
package com.cqan.digital.rest.impl;


import static com.cqan.util.http.HttpStatus.*;

import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.HttpHeaders;

import org.springframework.stereotype.Component;

import com.cqan.digital.entity.Digital;
import com.cqan.digital.entity.resp.DigitalPageResp;
import com.cqan.digital.entity.resp.DigitalResp;
import com.cqan.digital.rest.DigitalRestService;
import com.cqan.digital.service.DigitalService;
import com.cqan.log.entity.DigitalAccessLog;
import com.cqan.log.service.DigitalAccessLogService;
import com.cqan.orm.Page;
import com.cqan.orm.PropertyFilter;
import com.cqan.service.rest.BaseRestService;
import com.google.common.collect.Lists;

/**
 * @Description 数据节点REST接口
 * @author slaton.wu@gmail.com
 * @date 2012-7-5下午3:48:34
 *
 */
@Component("digitalRestService")
public class DigitalRestServiceImpl extends BaseRestService implements DigitalRestService {

	@Resource(name="digitalService")
	private DigitalService digitalService;
	
	@Resource(name="digitalAccessLogService")
	private DigitalAccessLogService digitalAccessLogService;
	
	@Override
	public DigitalResp getNodeInfo(Long id,HttpHeaders headers) {
		DigitalResp resp = new DigitalResp();
		try {
			Digital digital = digitalService.get(id);
			if (digital==null||!digital.isStatus()) {
				resp.setInfocode(NO_CONTENT);
			}else {
				resp.setDigital(digital);
				resp.setInfocode(OK);
				digitalAccessLogService.add(new DigitalAccessLog(headers, req.getRemoteAddr(), id));
			}
		} catch (Exception e) {
			resp.setInfocode(INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}

	
	@Override
	public DigitalPageResp getNodesByPid(Long id,Integer pageNo,Integer pageSize,HttpHeaders headers) {
		DigitalPageResp resp = null;
		try {
			Page<Digital> page = new Page<Digital>(pageSize);
			page.setPageNo(pageNo);
			List<PropertyFilter> filters = Lists.newArrayList();
			filters.add(new PropertyFilter("EQL_pid",String.valueOf(id)));
			filters.add(new PropertyFilter("EQB_status","true"));
			page = digitalService.search(page, filters);
			resp = new DigitalPageResp(page);
			resp.setData(page.getResult());
			if (resp.getData()!=null&&resp.getData().size()>0) {
				resp.setInfocode(OK);
				digitalAccessLogService.add(new DigitalAccessLog(headers, req.getRemoteAddr(), id));
			}else {
				resp.setInfocode(NO_CONTENT);
			}
		} catch (Exception e) {
			new DigitalPageResp();
			resp.setInfocode(INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}
}
